package com.cloudcc.mobile.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudcc.cloudframe.net.ErrorInfo;
import com.cloudcc.cloudframe.net.ResultCallBack;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.adapter.NewContactAdapter;
import com.cloudcc.mobile.dao.impl.ApprovalEngineImpl;
import com.cloudcc.mobile.entity.MultipleListmodel;
import com.cloudcc.mobile.view.base.BaseActivity;
import com.google.gson.JsonObject;
import com.mypage.model.ContactData;
import com.mypage.utils.AnimViewUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class NewContact extends BaseActivity implements ResultCallBack<MultipleListmodel> {
    public static NewContact instance = null;
    @Bind(R.id.topSynchLayout)
    RelativeLayout topSynchLayout;
    @Bind(R.id.tishiyu)
    TextView tishiyu;
    @Bind(R.id.tishitu)
    ImageView tishitu;
    @Bind(R.id.layoutTop)
    FrameLayout layoutTop;
    private TextView kongTv;
    private RelativeLayout topLayoutBackg;
    private TextView toastHintContent;
    private ImageView imgDeletes;
    //    @Bind(R.id.load_more_list_view_ptr_frame)
    //    PtrFrameLayout mRefreshLayout;
    private TextView num, paixu;
    private ImageView returnImageView, newcontact_xinjian;
    private LinearLayout newcontact_tishi;
    private ListView listView;
    private ApprovalEngineImpl engine = new ApprovalEngineImpl();
    private String layoutId;// 布局id
    private String recordId;// 记录id
    private String relatedlistId;// 相关列表id
    private String tag = "";
    private PopupWindow mSearchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_contact);
        ButterKnife.bind(this);
        instance = this;
        initView();
        //        initrefre();
        //        initRequestData();
        addListener();
        initdata();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_new_contact;
    }

    private void initdata() {
        //        listView.setOnRefreshOrLoadMoreListener(this);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("layoutId", layoutId);
        jsonObject.addProperty("recordId", recordId);
        jsonObject.addProperty("relatedlistId", relatedlistId);
        engine.GetMultipleRequest(jsonObject, NewContact.this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if("".equals(tag)){
            layoutTop.setVisibility(View.GONE);
        }else{
            tag = "";
        }

        newcontact_xinjian.setImageDrawable(getResources().getDrawable(R.drawable.contactpersonrole_new_default));
        returnImageView.setImageDrawable(getResources().getDrawable(R.drawable.fujin_back));

    }

    private void addListener() {
        returnImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                returnImageView.setImageDrawable(getResources().getDrawable(R.drawable.fujin_back));
                finish();
            }
        });
        newcontact_xinjian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                newcontact_xinjian.setImageDrawable(getResources().getDrawable(R.drawable.contactpersonrole_new_pressed));
                Intent intent = new Intent(NewContact.this, CreatContact.class);
                intent.putExtra("recordId", recordId);
                intent.putExtra("layoutId", layoutId);
                intent.putExtra("relatedlistId", relatedlistId);
                startActivity(intent);
            }
        });

    }

    private String noeName = "";//当前编辑删除保存的 name
    private String ischengg = "";

    private void initView() {

        newcontact_xinjian = (ImageView) findViewById(R.id.newcontact_xinjian);
        num = (TextView) findViewById(R.id.contact_num);
        paixu = (TextView) findViewById(R.id.contact_paixu);
        returnImageView = (ImageView) findViewById(R.id.Contact_return);
        listView = (ListView) findViewById(R.id.contact_list);
        newcontact_tishi = (LinearLayout) findViewById(R.id.newcontact_tishi);
        kongTv = (TextView) findViewById(R.id.kongbuju);
        topLayoutBackg = (RelativeLayout) findViewById(R.id.topLayoutBackg);
        Intent intent = getIntent();
        layoutId = intent.getStringExtra("layoutId");
        recordId = intent.getStringExtra("recordId");
        relatedlistId = intent.getStringExtra("relatedlistId");
        tag = intent.getStringExtra("tag");
        noeName = intent.getStringExtra("name");
        ischengg = intent.getStringExtra("chengg");
            newcontact_xinjian.setImageDrawable(getResources().getDrawable(R.drawable.contactpersonrole_new_default));
        returnImageView.setImageDrawable(getResources().getDrawable(R.drawable.fujin_back));
        if (null!= tag) {
            toastHintContent = (TextView) findViewById(R.id.toastHintContent);
            imgDeletes = (ImageView) findViewById(R.id.imgDeletes);
            imgDeletes.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    imgDeletes.setImageDrawable(getResources().getDrawable(R.drawable.contactpersonrole_close_pressed));
                    layoutTop.setVisibility(View.GONE);
                }
            });
            if ("bianji".equals(tag)) {
                if ("chengg".equals(ischengg)) {
                    topLayoutBackg.setBackground(getResources().getDrawable(R.drawable.toast_hint_beau));
                    toastHintContent.setText(getString(R.string.ContactList)+"“" + noeName + getString(R.string.successbianji));
                } else {
                    topLayoutBackg.setBackground(getResources().getDrawable(R.drawable.toast_hint_beau));
                    toastHintContent.setText(getString(R.string.ContactList)+"“"  + noeName + getString(R.string.bianjifaile));
                }

            } else if ("shanchu".equals(tag)) {
                topLayoutBackg.setBackground(getResources().getDrawable(R.drawable.toast_hint_beau));
                toastHintContent.setText(getString(R.string.ContactList)+"“"  + noeName + getString(R.string.shanchucon));
            } else if ("xinjian".equals(tag)) {
                if ("chengg".equals(ischengg)) {
                    topLayoutBackg.setBackground(getResources().getDrawable(R.drawable.toast_hint_beau));
                    toastHintContent.setText(getString(R.string.ContactList)+"“"  + noeName + getString(R.string.baocuncon));
                } else {
                    topLayoutBackg.setBackground(getResources().getDrawable(R.drawable.toast_hint_beau));
                    toastHintContent.setText(getString(R.string.creatcontact)+"“" + noeName + getString(R.string.shibaicon));
                }

            }
            topLayoutBackg.setVisibility(View.GONE);
            layoutTop.setVisibility(View.VISIBLE);
            new AnimViewUtils().appearToast(topLayoutBackg);

        }

    }

    List<ContactData> list = null;
    private NewContactAdapter adapter = null;

    @Override
    public void onSuccess(MultipleListmodel data) {
        list = new ArrayList<>();
        List<JsonObject> valuelist = data.getRelationData();
        try {
            for (int i = 0; i < valuelist.size(); i++) {
                ContactData contactData = new ContactData();
                try {
                    contactData.juese = valuelist.get(i).get("rolename").getAsString();
                } catch (Exception ex) {
                    contactData.juese = "";
                }
                try {
                    contactData.dianhua = valuelist.get(i).get("dianhua").getAsString();
                } catch (Exception ex) {
                    contactData.dianhua = "";
                }
                try {
                    contactData.name = valuelist.get(i).get("contactname").getAsString();
                } catch (Exception ex) {
                    contactData.name = "";
                }
                try {
                    contactData.id = valuelist.get(i).get("id").getAsString();
                } catch (Exception ex) {
                    contactData.id = "";
                }
                try {
                    contactData.zhuwu = valuelist.get(i).get("zhiwu").getAsString();
                } catch (Exception ex) {
                    contactData.zhuwu = "";
                }
                try {
                    contactData.kehu_name = valuelist.get(i).get("accountname").getAsString();
                } catch (Exception ex) {
                    contactData.kehu_name = "";
                }

                try {
                    contactData.kehu_id = valuelist.get(i).get("accountid").getAsString();
                } catch (Exception ex) {
                    contactData.kehu_id = "";
                }
                try {
                    contactData.creatName = valuelist.get(i).get("createbyname").getAsString();
                } catch (Exception ex) {
                    contactData.creatName = "";
                }
                try {
                    contactData.creatId = valuelist.get(i).get("createbyid").getAsString();
                } catch (Exception ex) {
                    contactData.creatId = "";
                }
                try {
                    contactData.creatTime = valuelist.get(i).get("createdate").getAsString();
                } catch (Exception ex) {
                    contactData.creatTime = "";
                }
                try {
                    contactData.isdefault = valuelist.get(i).get("isdefault").getAsString();
                } catch (Exception ex) {
                    contactData.isdefault = "";
                }
                try {
                    contactData.lianxiId = valuelist.get(i).get("contactid").getAsString();
                } catch (Exception ex) {
                    contactData.lianxiId = "";
                }
                try {
                    contactData.jueseId = valuelist.get(i).get("role").getAsString();
                } catch (Exception ex) {
                    contactData.jueseId = "";
                }
                try {
                    contactData.accountpermiss = valuelist.get(i).get("accountpermiss").getAsString();
                } catch (Exception ex) {
                    contactData.accountpermiss = "0";
                }
                list.add(contactData);
            }
        } catch (Exception ex) {

        }

        if (list.size() <= 0) {
            newcontact_tishi.setVisibility(View.GONE);
            kongTv.setVisibility(View.VISIBLE);
        } else {
            kongTv.setVisibility(View.GONE);
            num.setText(list.size() + " ");
            newcontact_tishi.setVisibility(View.VISIBLE);
        }
        //        if (adapter == null) {
        adapter = new NewContactAdapter(NewContact.this, list, recordId, layoutId, relatedlistId);
        listView.setAdapter(adapter);
        //        } else {

        //        }

    }
    @Override
    public void onFailure(ErrorInfo error) {
        Toast.makeText(NewContact.this, error.getErrorMessage(),
                Toast.LENGTH_LONG).show();

    }
}
